package com.gzz.demo;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

public class Test {
    static Map<Integer, LinkedBlockingQueue<String>> queueMap=new HashMap();

    /**
     * 事先设置好队列
     *
     * 队列下标和单号的hash算法获取index对应。
     */
    static{
        queueMap.put(0,new LinkedBlockingQueue<String>(1000));
        queueMap.put(1,new LinkedBlockingQueue<String>(1000));
        queueMap.put(2,new LinkedBlockingQueue<String>(1000));
    }

    public void doConsumer(int index){
        LinkedBlockingQueue<String> queue = queueMap.get(index);
        int size = queue.size();
        while(queue.size()!=0){
//            System.out.println("队列下标："+index+"-->队列大小："+queue.size());
            String take = null;
            try {
                take = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 业务逻辑处理
//            System.out.println("当前获取的状态："+take.toString()+" 线程："+Thread.currentThread().getName());
//            Thread02 ship_id = thread02Service.getOne(new QueryWrapper<Thread02>().eq("ship_id", take.getShipId()));
//            Thread02 t02=new Thread02();
//            if(null==ship_id){
//                t02.setShipId(take.getShipId());
//                t02.setStatus(take.getStatus());
//                thread02Service.save(t02);
//            }else{
//                thread02Service.update(new UpdateWrapper<Thread02>().eq("ship_id",take.getShipId()).set("status",take.getStatus()));
//            }
        }
    }
    @PostConstruct
    public void doThread(){

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 循环消费数据。线程绑定队列
        for (int i = 0; i < 3; i++) {
            int  index=i;
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("-----------初始化线程任务-----------------");
                    while(true){
                        doConsumer(index);
                    }
                }
            },"thread_"+index);
        }
    }
    /***********************************************************************************************************************/

    public void consumer() {
        // 模拟上游传递过来的数据
//        List<Thread01> thread01s = list();
//        int size = thread01s.size();
//        for (int i = 0; i < size; i++) {
//            Thread01 thread01 = thread01s.get(i);
//            System.out.println("当前获取的状态："+thread01.toString());
//            chooseQueue(thread01);
//        }
    }

    /**
     * 通过单号选择进入那个队列中
     * @param thread01
     */
//    private void chooseQueue(Thread01 thread01) {
//        String shipId = thread01.getShipId();
//        int index = Math.abs(shipId.hashCode()) % 3;
//        putQueue(index,thread01);
//    }
//
//    private void putQueue(int index, Thread01 thread01)  {
//        LinkedBlockingQueue<Thread01> queue = queueMap.get(index);
//        try {
//            queue.put(thread01);
//            System.out.println(index+"-->"+queue.size());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

}
